/*
 * @Author: cmd 
 * @wechat: cmd_develop 
 * @Date: 2019-05-16 11:12:31 
 * @Last Modified by: cmd
 * @Last Modified time: 2019-05-16 23:33:50
 * @Description: 
 */

import orc from "../../../orc";
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    enableScroll:boolean = true;//允许排行榜滚动
    time_count:number=0;
    rendInterval:number = 0.5;//刷新排行画布间隔s
    rankTexture:cc.Texture2D=null;
    rankSpriteFrame : cc.SpriteFrame=null;
    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    rank_display_zone:cc.Node = null;

    @property(cc.Node)
    rank_move_zone:cc.Node = null;

    onLoad () {
        this.rankTexture = new cc.Texture2D();
        this.rankSpriteFrame = new cc.SpriteFrame();
        this.resizeSharedCanvas(this.rank_display_zone.width, this.rank_display_zone.height);
    }

    start () {
        orc.platform_manager.platform.display_rank('total_rank');
    }

    onEnable(){
        if (this.enableScroll) {
            this.rank_move_zone.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        }
    }

    onTouchMove(event) {
        const deltaY = event.getDeltaY();
        console.log("rank touchmove:", deltaY);        
        orc.platform_manager.platform.postMessage("scrolling", deltaY)
    }

   

     /**
     * 设置开放域的高、宽
     * @param width 宽
     * @param height 高
     */
    resizeSharedCanvas(width, height){        
        let sharedCanvas = window["wx"].getOpenDataContext().canvas;
        sharedCanvas.width = width
        sharedCanvas.height = height        
    }

    updateRankList() {        
        if(!this.rankTexture) return;
        let sharedCanvas = window["wx"].getOpenDataContext().canvas
        this.rankTexture.initWithElement(sharedCanvas);
        this.rankTexture.handleLoadedTexture();
        this.rankSpriteFrame.setTexture(this.rankTexture);
        this.rank_display_zone.getComponent(cc.Sprite).spriteFrame = this.rankSpriteFrame;        
    }

    btn_close(){
        orc.win_manager.win_close('win_rank',0);
    }

    update (dt) {
        this.time_count = this.time_count + dt;
        if(this.time_count >= 1/20)
        {
            this.time_count = 0;
            this.updateRankList();
        }
        
    }
}
